package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfx.entity.*;
import com.wfx.mapper.SysRoleMapper;
import com.wfx.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper , SysRole> implements ISysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper ;

    // 查询
    @Override
    public PageResult<SysRole> search(PageDTO<SysRole> pageDTO) {
        if (pageDTO == null) {
            return new PageResult<SysRole>(false,"查询失败",null,null,null);
        }
        IPage<SysRole> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());

        SysRole data = pageDTO.getData();

        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>(pageDTO.getData());
        if (!StringUtils.isEmpty(data.getRoleName())){
            queryWrapper.eq("role_name",data.getRoleName());
        }
        IPage<SysRole> pageInfo = sysRoleMapper.selectPage(iPage,queryWrapper);

        PageResult pageResult = new PageResult<>();
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;
    }

    //添加or修改
   @Override
    public Result addOrUpdate(SysRole sysRole) {
        if (sysRole == null) {
            return new Result(false,"参数异常");
        }
        if(!StringUtils.isEmpty(sysRole.getRoleCode())){
            int i = sysRoleMapper.updateById(sysRole);
            return  new Result(i==1?true:false,i==1?"修改成功":"修改失败");
        }else {
            int i = sysRoleMapper.insert(sysRole);
            return  new Result(i==1?true:false,i==1?"添加成功":"添加失败");
        }
    }

    // 批量删除
    @Override
    public Result delBrands(List<String> roleCodes) {
        if (roleCodes == null) {
            return new Result(false,"参数异常");
        }
        int i = sysRoleMapper.deleteBatchIds(roleCodes);
        return  new Result(i>0?true:false,i>0?"删除成功":"删除失败");
    }

    // 删除
    @Override
    public Result delOne(SysRole sysRole) {
        if (sysRole == null) {
            return new Result(false,"参数异常");
        }
        int i = sysRoleMapper.deleteById(sysRole);
        System.out.println(i);
        return  new Result(i==1?true:false,i==1?"删除成功":"删除失败");
    }
}
